<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\PublicController;
use App\Models\AuthRole;
use App\Models\AuthRule;
use App\Utils\CodeUtil;
use App\Utils\ConstantUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RuleController extends Controller
{
    public function index(){
        $list = AuthRule::orderBy("sort","desc")->get()->toArray();
        $tree = \App\Http\Controllers\AuthController::generate_tree($list);
        return view('Api/rule/index',["list"=>$tree]);
    }

    public function create(Request $request)
    {
        $parent_id = $request->input("parent_id");
        $list = AuthRule::find($parent_id);
        if (is_null($list)) {
            $list = [
                'level' => 1,
                'id'    => $parent_id,
                'title' => '无'
            ];
        } else {
            $list['level'] = $list['level'] + 1;
        }
        return view('Api/rule/create',["list"=>$list,"status"=>PublicController::$gx_init]);
    }
    /**
     * 保存新建的资源
     */
    function save(Request $request)
    {
        $input = $request->input();
        $obj = new AuthRule();
        foreach($input as $key => $val) {
            $obj->$key = $val;
        }
        $result = $obj->save();
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "添加成功!"]);
    }

    /**
     * 显示编辑资源表单页.
     */
    function edit(Request $request)
    {
        $list = AuthRule::find($request->input("id"));
        $parent = AuthRole::find($list['parent_id']);
        $parent_list = AuthRule::where("level",$list['level']-1)->get()->toArray();
        $list['parent'] = $parent['title'] == null ? '无' : $parent['title'];
        return view('Api/rule/edit',[
            "status" => ConstantUtil::getStatus(),
            'list'  => $list,
            'parent_list'  => $parent_list,
        ]);
    }

    /**
     * 保存更新的资源
     */
    function update(Request $request)
    {
        $id = $request->input("id");
        $data = $request->input();
        unset($data["id"]);
        AuthRule::where("id",$id)->update($data);
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "保存成功!"]);
    }

    /**
     * 删除指定资源
     */
    public function delete(Request $request)
    {
        $id = $request->input("id");
        if (!is_array($id)) $id = [$id];
        $delIds = $this->batch_delete($id);
        AuthRule::whereIn("id",$delIds)->delete();
        return response()->json(['code'=> CodeUtil::SUCCESS,"msg"=> "删除成功!"]);
    }

    /**
     * 获取子ID
     * 删除父类，相应的子类需要自动删除
     * @return array
     */
    private function batch_delete($ids)
    {
        $count  = count($ids);
        $list   = AuthRule::whereIn("parent_id", $ids)->get()->toArray();
        $sonIds = array_column($list, 'id');
        $allIds = array_merge($ids, $sonIds);
        $allIds = array_unique($allIds);
        if ($count != count($allIds)) $this->batch_delete($allIds);
        return $allIds;
    }





}
